import 'package:casdon/app/config/theme/theme_extention.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class SingleSelectTab extends StatefulWidget {
  final ValueChanged<int>? onChanged; // 选中回调
  const SingleSelectTab({super.key, this.onChanged});

  @override
  State<SingleSelectTab> createState() => _SingleSelectTabState();
}

class _SingleSelectTabState extends State<SingleSelectTab> {
  int _selectedIndex = 0;
  DateTime _lastTap = DateTime.fromMillisecondsSinceEpoch(0);

  @override
  Widget build(BuildContext context) {
    return Row(
      mainAxisSize: MainAxisSize.min,
      children: [
        _buildTabItem('最热', 0),
        SizedBox(width: 10.w),
        Container(
          width: 2.w,
          height: 16.h,
          color: context.customColorThemeNonNull.gy4,
        ),
        SizedBox(width: 10.w),
        _buildTabItem('最新', 1),
      ],
    );
  }

  Widget _buildTabItem(String label, int index) {
    final isSelected = _selectedIndex == index;
    return GestureDetector(
      onTap: () {
        final now = DateTime.now();
        if (now.difference(_lastTap).inSeconds < 2) return;
        _lastTap = now;
        setState(() {
          _selectedIndex = index;
        });
        widget.onChanged?.call(index); // 回调外部
      },
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          Text(
            label,
            style: isSelected
                ? context.textTheme.bodyLarge!.copyWith(
                    color: context.colorScheme.primary,
                  )
                : context.textTheme.bodyMedium!.copyWith(
                    color: context.customColorThemeNonNull.textColor2,
                  ),
          ),
        ],
      ),
    );
  }
}
